package com.example.composerecompose.screens

import android.util.Log
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ListItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.composerecompose.data.Movie
import com.example.composerecompose.data.buildMovies
import com.example.composerecompose.screens.shared.RefreshAction
import com.example.composerecompose.screens.shared.Screen
import com.example.composerecompose.screens.shared.TopBar

@Composable
fun LazyListKeys() {
    Screen {
        var movies by remember { mutableStateOf(buildMovies(4)) }
        Scaffold(
            topBar = {
                TopBar("LazyList Keys") {
                    RefreshAction { movies = movies.shuffled() }
                }
            },
        ) { padding ->
            LazyColumn(
                contentPadding = padding
            ) {
                items(movies, key = { it.id }) {
                    MovieItem(movie = it).apply {
                        Log.e("TAG_COMPOSE", "LazyListKeys:  42------", )
                    }
                }

//                items(movies) {
//                    MovieItem(movie = it).apply {
//                        Log.e("TAG_COMPOSE", "LazyListKeys:  42------", )
//                    }
//                }

            }
        }
    }
}

@Composable
fun MovieItem(movie: Movie) {
    ListItem(
        headlineContent = { Text(movie.title) },
        supportingContent = { Text(movie.description) },
        leadingContent = {
            AsyncImage(
                model = movie.image,
                contentDescription = movie.title,
                modifier = Modifier
                    .size(56.dp)
                    .clip(CircleShape)
            )
        }
    )
}

// TODO: 该类优化的是 items 的 key

